<template>
  <div class="yrfm-content-tool">
    <h2><icon-svg name="iconResTeam"></icon-svg>{{ config.rfmAnalysisName }}</h2>
    <div class="analyse-handler clearfix">
      <template v-if="type === 'asideMine'">
        <span class="release-tips" v-if="config.releaseStatus === 2">发布审核中</span>
        <el-button size="mini" @click="handleEdit" :class="{ disabled: config.releaseStatus === 2 }">
          <icon-svg name="iconFacEdit"></icon-svg>
          编辑
        </el-button>
        <el-button size="mini" @click="handleDelete">
          <icon-svg name="iconDelete"></icon-svg>
          删除
        </el-button>
      </template>
      <el-button size="mini" @click="handleCopy">
        <icon-svg name="iconCopy"></icon-svg>
        复制
      </el-button>
      <template v-if="type === 'asideMine'">
        <el-button size="mini" @click="handleMove">
          <icon-svg name="iconMove"></icon-svg>
          移动
        </el-button>
        <el-button size="mini" @click="handleRelease">
          <icon-svg :name="config.releaseStatus ? 'iconRedo' : 'iconRelease'"></icon-svg>
          {{ config.releaseStatus ? '取消发布' : '发布' }}
        </el-button>
        <!-- <el-button size="mini" @click="hendleRun" v-if="config.tagReqStatus !== 1">
          <icon-svg name="iconExport"></icon-svg>
          重新运行
        </el-button> -->
        <el-button size="mini" @click="handleDownload" :class="{ disabled: config.fileStatus != 1 }">
          <icon-svg name="iconExport"></icon-svg>
          导出结果
        </el-button>
      </template>
    </div>
    <yrfm-operation-dialog ref="yrfmOperationDialog" @callback="$emit('rollback')"></yrfm-operation-dialog>
  </div>
</template>

<script>
import yrfmOperationDialog from '../components/yrfm-operation-dialog'
export default {
  props: {
    config: {
      type: Object,
      default: () => {
        return {}
      }
    },
    type: {
      type: String,
      default: 'asideMine'
    }
  },
  components: { yrfmOperationDialog },
  data() {
    return {}
  },
  methods: {
    handleRelease() {
      this.$confirm(`确定${this.config.releaseStatus ? '取消发布' : '发布'}【${this.config.rfmAnalysisName}】？`).then(
        () => {
          let messageBox = document.getElementsByClassName('el-message-box__wrapper')
          messageBox[0].focus() // 防止按空格键重复确定
          this.$utils
            .release(
              this.config.tagId,
              this.config.rfmAnalysisName,
              '90',
              !this.config.releaseStatus ? 'post' : 'delete'
            )
            .then(data => {
              this.$message.success(data.msg)
              this.$emit('rollback')
            })
            .catch(data => {
              this.$message.error(data.data || data.msg)
            })
        }
      )
    },
    handleEdit() {
      if (this.config.releaseStatus == 2) {
        this.$message.info('分析正在审核中！')
        return
      }
      this.$router.push({ name: 'yrfm-config', query: { tagId: this.config.tagId, groupId: this.config.groupId } })
    },
    handleCopy() {
      this.$refs.yrfmOperationDialog.showDialog({
        tagId: this.config.tagId,
        tagNm: this.config.rfmAnalysisName,
        type: 'copy'
      })
    },
    handleMove() {
      this.$refs.yrfmOperationDialog.showDialog({
        tagId: this.config.tagId,
        type: 'move'
      })
    },
    handleDelete() {
      this.$confirm(`确定删除【${this.config.rfmAnalysisName}】？`).then(() => {
        let messageBox = document.getElementsByClassName('el-message-box__wrapper')
        messageBox[0].focus() // 防止按空格键重复确定
        this.$emit('setLoading', true)
        this.$ajax({
          url: this.$ajax.setUrl(`rfmAnalysis/deleteRfmAnalysis/${this.config.tagId}`),
          method: 'delete',
          params: this.$ajax.setParams()
        }).then(({ data }) => {
          if (data && data.code === '0') {
            this.$emit('deleteData')
            this.$message.success(data.msg)
          } else {
            this.$message.error(data.data || data.msg)
          }
          this.$emit('setLoading', false)
        })
      })
    },
    hendleRun() {
      this.$confirm(`确定重新运行【${this.config.rfmAnalysisName}】？`).then(() => {
        let messageBox = document.getElementsByClassName('el-message-box__wrapper')
        messageBox[0].focus() // 防止按空格键重复确定
        this.$emit('setLoading', true)
        this.$ajax({
          url: this.$ajax.setUrl(`DiffCustomerPortraits/getRestartDiff/${this.config.tagId}`),
          method: 'get',
          params: this.$ajax.setParams()
        }).then(({ data }) => {
          if (data && data.code === '0') {
            this.$emit('rollback')
            this.$message.success(data.msg)
          } else {
            this.$message.error(data.data || data.msg)
          }
          this.$emit('setLoading', false)
        })
      })
    },
    handleDownload() {
      if (this.config.fileId && this.config.fileStatus == 1) this.$dlFile(this.config.fileId)
    }
  }
}
</script>

<style lang="scss">
.tag-yrfm {
  .yrfm-content-tool {
    height: 50px;
    padding: 10px 0;
    h2 {
      color: #465fbc;
      padding-left: 15px;
      float: left;
      position: relative;
      &::before {
        width: 3px;
        content: '';
        background-color: #465fbc;
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
      }
      svg {
        margin-right: 10px;
      }
    }
    .analyse-handler {
      float: right;
      text-align: right;
      .release-tips {
        color: $tag-red;
      }
      .el-button {
        font-size: 15px;
        color: #455fbc;
        background-color: transparent;
        border-color: transparent;
        &.disabled {
          color: #333;
        }
      }
      svg {
        width: 17px;
        height: 17px;
        margin-right: 3px;
        margin-top: -5px;
        vertical-align: middle;
      }
    }
  }
}
</style>
